package cn.yunhe.controller;

import cn.yunhe.service.UserRedPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zs on 2018/1/8.
 */
@Controller
@RequestMapping("/userRedPackage")
public class UserRedPackageController {

    @Autowired
    private UserRedPackageService userRedPackageService = null;

    @RequestMapping("/grapRedPackage")
    public Map<String, Object> grapRedPackage(Long redPackageId, Long userId) {
        //抢红包
        int result = userRedPackageService.grapRedPackage(redPackageId, userId);
        Map<String, Object> retMap = new HashMap<String, Object>();
        boolean flag = result > 0;
        retMap.put("success", flag);
        retMap.put("message", flag ? "抢红包成功" : "抢红包失败");
        return retMap;

        //可通过以下sql检验效果
        /*
        select a.amount,a.stock,id from t_red_packet a where id=1
        union all
        select sum(b.amount),count(*),max(user_id) from t_user_red_packet b where b.red_packet_id=1;
        //红包发送时间
        select min(grab_time),max(grab_time),count(*) from t_user_red_packet b where red_packet_id=1
        */
    }

    public UserRedPackageService getUserRedPackageService() {
        return userRedPackageService;
    }

    public void setUserRedPackageService(UserRedPackageService userRedPackageService) {
        this.userRedPackageService = userRedPackageService;
    }
}
